package org.example.hadoop.service.authorSort;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.example.hadoop.domain.AuthorCountBean;

import java.io.IOException;

/**
 * @Author: wwy
 * @Date: 2020/11/10 11:44
 */
public class AuthorSortMapper extends Mapper<LongWritable, Text, AuthorCountBean, Text> {
    /**
     * k1     v1
     *        余华 4
     * k2                   v2
     *
     * AuthorCountBean      余华,aa,bbb,ccc,ddd
     * @param key
     * @param value
     * @param context
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line = value.toString();
        String[] valueArray = line.split("\t");
        String authorName = valueArray[0];
        int count = Integer.parseInt(valueArray[1]);
        AuthorCountBean authorCountBean = new AuthorCountBean();
        authorCountBean.setCount(count);
        context.write(authorCountBean,new Text(authorName));
    }
}
